﻿#include <Windows.h>

#define ClassName "day day up"
#define WindowName  "爱白菜的小昆虫"

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_KEYUP:
		switch (wParam)
		{
		case VK_F1:
			SetWindowText(hWnd, "VK_F1");
			break;
		case VK_F2:
			SetWindowText(hWnd, "VK_F2");
			break;
		default:
			break;
		}
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

INT APIENTRY wWinMain(
    _In_ HINSTANCE,
    _In_opt_ HINSTANCE,
    _In_ LPWSTR,
    _In_ INT)
{
	// 1.定义窗口类
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(wcex);//指定结构体的大小
	wcex.style = CS_CLASSDC;//结构体的大小，‌用于版本控制。‌
	wcex.lpfnWndProc = MsgProc;//指向窗口过程的指针，‌处理窗口的各种消息。‌
	wcex.cbClsExtra = 0;//窗口类附加内存的大小，‌通常设为0。‌
	wcex.cbWndExtra = 0;//窗口附加内存的大小，‌通常设为0。‌
	wcex.hInstance = GetModuleHandle(NULL);//应用程序实例句柄
	wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);//窗口类的光标句柄
	//wcex.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(100, 100, 100));//窗口背景画刷句柄
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);  // 使用默认的背景颜色
	wcex.lpszMenuName = nullptr;//窗口类的菜单
	wcex.lpszClassName = ClassName;//窗口类名
	wcex.hIcon = nullptr;//类图标的句柄
	wcex.hIconSm = nullptr;//小图标的句柄

	// 2.注册窗口类
	RegisterClassEx(&wcex);//注册本地窗口

	INT WindowWidth = 640;
	INT WindowHeight = 480;
	RECT rect;
	rect.left = rect.top = 0;
	rect.right = WindowWidth;
	rect.bottom = WindowHeight;
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);//根据所需的客户端矩形大小计算窗口矩形的所需大小

	INT cx = GetSystemMetrics(SM_CXSCREEN);
	INT cy = GetSystemMetrics(SM_CYSCREEN);
	// 3.创建窗口
	HWND hWnd = CreateWindowEx(
		0,//扩展窗口样式
		ClassName,//窗口类名或窗口类原子的指针
		WindowName,//窗口名
		WS_OVERLAPPEDWINDOW,//窗口样式
		cx / 2 - WindowWidth / 2,//显示窗口位置
		cy / 2 - WindowHeight / 2,
		rect.right - rect.left,//创建窗口大小
		rect.bottom - rect.top,
		HWND_DESKTOP,//没有父窗口
		nullptr,//菜单句柄或子窗口ID
		wcex.hInstance,//应用程序实例句柄
		nullptr//指向窗口创建数据的指针
	);
	
	if (nullptr == hWnd)
		return -1;
	// 4.显示窗口
	ShowWindow(hWnd, SW_SHOW);//设置指定窗口的显示状态
	UpdateWindow(hWnd);

	MSG msg{ 0 };
	// 5.获取消息
	while (GetMessage(&msg, NULL, 0U, 0U)) {
		TranslateMessage(&msg);//将虚拟密钥消息转换为字符消息
		DispatchMessage(&msg);//分发消息给窗口‌
	}

	UnregisterClass(ClassName, wcex.hInstance);
    return 0;
}
